﻿using DataWasher.WebApi.Repositories;
using Microsoft.Extensions.DependencyInjection;
using Report.WebApi.Interfaces;
using Report.WebApi.Models;
using Report.WebApi.Services.Common;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using ToolManager.Common;

namespace Report.WebApi.Services
{
    public class QueryService
    {
        private readonly ISysDomainRepository _repository;
        public readonly ConnectionStrings _connectionStrings;
        public QueryService(ISysDomainRepository repository)
        {
            _repository = repository;
            _connectionStrings = ServiceLocator.Instance.GetService<ConnectionStrings>();
        }

        public virtual async Task<string> ParamBuilder(QueryRequestModel request, YearMonthModel filterModel)
        {
            throw new NotImplementedException();
        }
        public async Task<YearMonthModel> GetMonthsThisYear(QueryRequestModel request)
        {
            var yearModel = new YearMonthModel();
            yearModel.Items = new List<int>();
            yearModel.Quarters = new List<Quarter>();

            if (request.QueryByYear || request.QueryHomePage)
            {
                var year = request.QueryYear;
                yearModel.Year = year;
                //var month = request.QueryYear < DateTime.Now.Year ? 12 : (request.QueryMonth == 0 ? DateTime.Now.Month : request.QueryMonth);
                var month = request.QueryYear < DateTime.Now.Year ? 12 : DateTime.Now.Month;
                for (int i = 1; i <= month; i++)
                {
                    yearModel.Items.Add(i);
                }
                yearModel.QueryBegin = int.Parse(request.QueryYear < DateTime.Now.Year ? $"{request.QueryYear}01" : DateTime.Now.AddMonths(0 - yearModel.Items.Count + 1).ToString("yyyyMM"));
                yearModel.QueryEnd = int.Parse(request.QueryYear < DateTime.Now.Year ? $"{request.QueryYear}12" : DateTime.Now.ToString("yyyyMM"));

                yearModel.QueryBeginYoY = int.Parse(request.QueryYear < DateTime.Now.Year ? $"{request.QueryYear - 1}01" : DateTime.Now.AddYears(-1).AddMonths(0 - yearModel.Items.Count + 1).ToString("yyyyMM"));
                yearModel.QueryEndYoY = int.Parse(request.QueryYear < DateTime.Now.Year ? $"{request.QueryYear - 1}12" : DateTime.Now.AddYears(-1).ToString("yyyyMM"));

                //yearModel.QueryBeginDP = int.Parse(request.QueryYear < DateTime.Now.Year ? $"{request.QueryYear - 1}-01" : DateTime.Now.AddYears(-1).AddMonths(0 - yearModel.Months.Count + 1).ToString("yyyyMM"));
                //yearModel.QueryEndDP = int.Parse(request.QueryYear < DateTime.Now.Year ? $"{request.QueryYear - 1}-12" : DateTime.Now.AddYears(-1).ToString("yyyyMM"));

            }

            if (request.QueryByMonth)
            {
                yearModel.Year = request.QueryYear;
                var days = DateTime.DaysInMonth(request.QueryYear, request.QueryMonth);
                for (int i = 1; i <= days; i++)
                {
                    yearModel.Items.Add(i);
                }
                yearModel.QueryBegin = int.Parse($"{request.QueryYear}{request.QueryMonth}01");
                yearModel.QueryEnd = int.Parse($"{request.QueryYear}{request.QueryMonth}{days}");

                yearModel.QueryBeginYoY = int.Parse($"{request.QueryYear - 1}{request.QueryMonth}01");
                yearModel.QueryEndYoY = int.Parse($"{request.QueryYear - 1}{request.QueryMonth}{days}");

            }
            //自定义查询，包括周查询和自定义查询
            if (request.QueryByCustom)
            {
                var startTime = DateTime.Parse(request.StartTime);
                var endTime = DateTime.Parse(request.EndTime);
                yearModel.Year = request.QueryYear;
                var days = (endTime - startTime).Days + 1;
                DateTime currentTime;
                for (int i = 0; i < days; i++)
                {
                    currentTime = startTime.AddDays(i);
                    yearModel.Items.Add(int.Parse($"{currentTime.Month}709112{currentTime.Day}"));
                }
                var montFmat = startTime.Month.ToString().Length == 1 ? $"0{startTime.Month}" : startTime.Month.ToString();
                var dayFamt = startTime.Day.ToString().Length == 1 ? $"0{startTime.Day}" : startTime.Day.ToString();

                yearModel.QueryBegin = int.Parse($"{startTime.Year}{montFmat}{dayFamt}");
                yearModel.QueryBeginYoY = int.Parse($"{startTime.Year - 1}{montFmat}{dayFamt}");

                montFmat = endTime.Month.ToString().Length == 1 ? $"0{endTime.Month}" : endTime.Month.ToString();
                dayFamt = endTime.Day.ToString().Length == 1 ? $"0{endTime.Day}" : endTime.Day.ToString();

                yearModel.QueryEnd = int.Parse($"{endTime.Year}{montFmat}{dayFamt}");
                yearModel.QueryEndYoY = int.Parse($"{endTime.Year - 1}{montFmat}{dayFamt}");

            }
            if (request.QueryByQuarter)
            {
                yearModel.Year = request.QueryYear;
                var months = await GetQuarterMonths(request.QueryQuarter);
                foreach (var mon in months)
                {
                    yearModel.Items.Add(mon);
                }
                yearModel.Quarters = await GetQuarters(request.QueryYear, request.QueryQuarter);


                yearModel.QueryBegin = int.Parse($"{request.QueryYear}{months[0]}01");
                yearModel.QueryEnd = int.Parse($"{request.QueryYear}{months[2]}{DateTime.DaysInMonth(request.QueryYear, months[2])}");

                yearModel.QueryBeginYoY = int.Parse($"{request.QueryYear - 1}{months[0]}01");
                yearModel.QueryEndYoY = int.Parse($"{request.QueryYear - 1}{months[2]}{DateTime.DaysInMonth(request.QueryYear, months[2])}");

            }

            return yearModel;
        }

        private async Task<List<Quarter>> GetQuarters(int year, int quarterIndex)
        {
            var quarters = new List<Quarter>();
            var months = await GetQuarterMonths(quarterIndex);

            foreach (var mon in months)
            {
                var days = DateTime.DaysInMonth(year, mon);
                quarters.Add(new Quarter()
                {
                    Month = mon,
                    StartTime = int.Parse($"{year}{mon}01"),
                    EndTime = int.Parse($"{year}{mon}{days}"),
                    YOYStartTime = int.Parse($"{year - 1}{mon}01"),
                    YOYEndTime = int.Parse($"{year - 1}{mon}{days}"),
                });
            }
            return quarters;

        }

        public async Task<List<int>> GetQuarterMonths(int index)
        {
            var months = new List<int>();
            switch (index)
            {
                case 1:
                    months = new List<int>() { 1, 2, 3 };
                    break;
                case 2:
                    months = new List<int>() { 4, 5, 6 };
                    break;
                case 3:
                    months = new List<int>() { 7, 8, 9 };
                    break;
                case 4:
                    months = new List<int>() { 10, 11, 12 };
                    break;
                default:
                    break;
            }
            return months;
        }



        public async Task<List<string>> GetDomainIds(QueryRequestModel request)
        {
            var regions = new List<DomainModel>();
            if (!string.IsNullOrEmpty(request.DomainId))
                regions = await _repository.GetDomainList(request.DomainId, "[dbo].[DGDomainNx]");
            else
                regions = await _repository.GetDomainList(request.OwnerId, "[dbo].[DGDomainNx]");
            return regions.Select(x => x.DomainId).ToList();
        }


    }
}
